package cn.wolfcode.web.controller;

import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.domain.MessageReply;
import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.qo.MessageBoardQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMessageBoardService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IMenuService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.utils.RequestPermission;
import com.github.pagehelper.PageInfo;
import org.aspectj.bridge.IMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/messageBoard")
public class MessageBoardController {
    @Autowired
    private IMessageBoardService messageBoardService ;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequestMapping("/list")
    @RequestPermission(name="留言板查询",expression = "messageBoard:list")
    public String list(Model model , MessageBoardQueryObject qo){

        List<SystemDictionary> catalogs = systemDictionaryService.selectByBs("business");
        model.addAttribute("catalogs",catalogs);

        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "messageBoard/list" ;
    }

    @RequestMapping("/replay")
    @RequestPermission(name="留言板查询",expression = "messageBoard:list")
    public String replay(Model model ,Long id){

        MessageBoard messageBoard = messageBoardService.selectById(id);
        //查回复
        List<MessageReply> messageReplies = messageBoardService.selectByReplay(id);

        int count = messageBoardService.selectCount(id);

        model.addAttribute("messageBoard",messageBoard);
        model.addAttribute("count",count);
        model.addAttribute("messageReplies",messageReplies);

        return "messageBoard/detail" ;
    }



    @RequestMapping("/delete")
    @RequestPermission(name="留言板删除",expression = "messageBoard:delete")
    public String delete(Long id){
        if (id != null) {
            messageBoardService.delete(id);
        }
        return "redirect:/messageBoard/list" ;
    }


    @RequestMapping("/input")
    public String input(MessageBoard messageBoard){
        messageBoardService.insert(messageBoard);
        return "redirect:/messageBoard/list";
    }

    @RequestMapping("/save")
    @RequestPermission(name="留言板编辑",expression = "messageBoard:save")
    public String save(MessageBoard messageBoard , Long id){
        if (id != null){
            messageBoardService.update(messageBoard);
            return "redirect:/messageBoard/list";
        }
        messageBoardService.insert(messageBoard);
        return "redirect:/messageBoard/list";
    }

    @RequestMapping("/queryByParentId")
    @ResponseBody
    public List<SystemDictionary> queryByParentId(Long parentId){
        return systemDictionaryService.queryByParentId(parentId) ;
    }
}
